package com.hslxy.malllearning.controller;

import com.hslxy.malllearning.common.api.CommonPage;
import com.hslxy.malllearning.common.api.CommonResult;
import com.hslxy.malllearning.mbg.model.PmsBrand;
import com.hslxy.malllearning.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
    @Autowired
    private PmsBrandService pmsBrandService;

    private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);

    @ApiOperation("获取所有品牌列表")
    @RequestMapping(value = "listAll", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult<List<PmsBrand>> getBrandList(){
        return CommonResult.success(pmsBrandService.listAllBrand());
    }

    @ApiOperation("添加品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:create')")
    public CommonResult<PmsBrand> createBrand(@RequestBody PmsBrand pmsBrand){
        int brand = pmsBrandService.createBrand(pmsBrand);
        if (brand == 1){
            LOGGER.debug("create pmsBrand sucess:{}", pmsBrand);
            return CommonResult.success(pmsBrand);
        }else {
            LOGGER.debug("create pmsBrand failed:{}", pmsBrand);
            return CommonResult.failed("创建失败");
        }
    }

    @ApiOperation("删除指定id品牌信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:delete')")
    public CommonResult deleteBrand(@PathVariable("id") Long id){
        int i = pmsBrandService.deleteBrand(id);
        if (i == 1){
            LOGGER.debug("delete pmsBrand sucess:{}", id);
            return CommonResult.success(id);
        }else {
            LOGGER.debug("delete pmsBrand fail:{}", id);
            return CommonResult.failed("删除失败");
        }
    }
    @ApiOperation("更新指定id品牌信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:update')")
    public CommonResult updateBrand(@PathVariable("id") Long id, @RequestBody PmsBrand pmsBrand, BindingResult result){
        int i = pmsBrandService.updateBrand(id, pmsBrand);
        if (i == 1){
            LOGGER.debug("update pmsBrand success:{}", pmsBrand);
            return CommonResult.success(pmsBrand);
        }else {
            LOGGER.debug("update pmsBrand fail:{}", pmsBrand);
            return CommonResult.failed("更新失败");
        }
    }

    @ApiOperation("分页查询品牌列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult<CommonPage<PmsBrand>> listBrand(
                                                            @RequestParam(value = "pageNum", defaultValue = "1")
                                                            @ApiParam("页码")
                                                            Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "3")
                                                              @ApiParam("每页数量")
                                                              Integer pageSize
                                                        ){
        List<PmsBrand> brands = pmsBrandService.listBrand(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(brands));
    }

    @ApiOperation("获取指定id的品牌详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAuthority('pms:brand:read')")
    public CommonResult<PmsBrand> listByid(@PathVariable("id") Long id){
        PmsBrand brand = pmsBrandService.getBrand(id);
        if (brand != null){
            return CommonResult.success(brand);
        }else {
            return CommonResult.failed("失败");
        }
    }

}
